//
// Created by 86176 on 2022/12/1.
//

#include "plc2llvm/SymbolRecord.h"
#include <iostream>

bool SymbolRecord::isIfExist() const {
    return ifExist;
}

void SymbolRecord::setIfExist(bool ifExist) {
    SymbolRecord::ifExist = ifExist;
}

int SymbolRecord::getAlternativeCount() const {
    return alternativeCount;
}

void SymbolRecord::setAlternativeCount(int count) {
    SymbolRecord::alternativeCount = count;
}

const std::string &SymbolRecord::getName() const {
    return name;
}

void SymbolRecord::setName(const std::string &name) {
    SymbolRecord::name = name;
}

const std::vector<PLCSymbol *> &SymbolRecord::getSymbolList() const {
    return alternativeSymbols;
}

SymbolRecord *SymbolRecord::makeRecord(std::string name, bool ifExist) {
    std::cout << "1" << std::endl;
    SymbolRecord* newRecord = new SymbolRecord();
    newRecord->setName(name);
    newRecord->setAlternativeCount(0);
    newRecord->setIfExist(ifExist);
    newRecord->setMajorSymbol(nullptr);
    std::cout << "2" << std::endl;
    return newRecord;
}

void SymbolRecord::linkRecord(SymbolRecord *another) {
    std::vector<PLCSymbol*> anotherSymbolList = another->getSymbolList();
    for(PLCSymbol* item : anotherSymbolList){
        int symbolId = item->getSymbolId();
        if(!this->idSet.count(symbolId)){
            this->alternativeSymbols.push_back(item);
            this->alternativeCount ++;
        }
    }
}


PLCSymbol *SymbolRecord::getFirstAlternativeSymbol() {
    return this->alternativeSymbols.at(0);
}

PLCSymbol *SymbolRecord::getMajorSymbol() const {
    return majorSymbol;
}

void SymbolRecord::setMajorSymbol(PLCSymbol *majorSymbol) {
    SymbolRecord::majorSymbol = majorSymbol;
    if (majorSymbol != nullptr)
        this->idSet.insert(majorSymbol->getSymbolId());
}

void SymbolRecord::addAlternativeSymbol(PLCSymbol *alternativeSymbol) {
    this->alternativeSymbols.push_back(alternativeSymbol);
    this->alternativeCount++;
}
